<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>修改密码</title>
    <link rel="stylesheet" href="/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/lib/layui/extend/formSelects-v4.css">
</head>
<body>
<div>
    <div class="layui-row" id="editDiv">
        <div class="layui-col-md6">
            <form id="editForm" class="layui-form layui-from-pane" lay-filter="editForm" action=""
                  style="margin-top:20px">
                <input id="id_txt" name="id" type="hidden"/>
                <div class="layui-form-item"><label class="layui-form-label">旧密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="password_txt" name="userName"
                               lay-verify="required" autocomplete="off" placeholder="请输入旧密码" class="layui-input"></div>
                </div>
                <div class="layui-form-item"><label class="layui-form-label">新密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="remark_txt" name="remark"
                               lay-verify="required" autocomplete="off"
                               placeholder="请输入新密码" class="layui-input">
                        <span style="color: red">*提示：密码长度8~16位，必须包含字母、数字</span>
                    </div>
                </div>
                <div class="layui-form-item"><label class="layui-form-label">重复新密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="passwordNew_txt" name="passwordNew"
                               lay-verify="required|equalCheck" autocomplete="off"
                               placeholder="请输入新密码" class="layui-input"></div>
                </div>
                <div class="layui-form-item" style="margin-top:40px">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-submit " lay-submit="" lay-filter="saveBtn">保存</button>
                        <button id="closeTab" type="button" class="layui-btn layui-btn-primary">关闭</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript" src="/js/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="/js/commonjs.js" charset="utf-8"></script>
<script src="/lib/layui/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'jquery', 'element'], function () {
        var form = layui.form;
        var $ = layui.$;
        var element = layui.element;

        var curtUser = layui.data('adminManager').curtUser;
        $("#id_txt").val(curtUser.id);

        form.on('submit(saveBtn)', function (form) {
            var newPw = $("#remark_txt").val();
            var repeatNewPw = $("#passwordNew_txt").val();
            var regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,16}');
            if (newPw != repeatNewPw) {
                layer.msg('两次新密码输入不一致', {time: 3000, icon: 2});
            } else if (!regex.test(newPw)) {
                layer.msg('密码长度8~16位，必须包含字母、数字', {time: 3000, icon: 2});
            } else {
                $.ajax({ // 注：这里必须是$.ajax 不能是$.post,其他页面同
                    url: '/systemUser/updatePassword',
                    dataType: 'json',
                    contentType: 'application/json', // 这句是必须的，不然java收不到，而$.post默认参数是application/x-www-form…… 且无法修改
                    type: 'post',
                    data: JSON.stringify(form.field),
                    success: function (data, status) {
                        if (data.code == 0) {
                            sessionStorage.setItem("simple", -1);
                            layer.msg("保存成功", {
                                icon: 6,
                                time: 3000
                            }, function () {
                                top.location.href = '/index';
                            });
                            // layer.msg('保存成功', {time: 3000, icon: 6});
                            // if (curtUser.loginName != 'admin')
                            //     parent.$('#closeEdit').click();
                        } else
                            layer.msg(data.msg, {icon: 2});
                    },
                    error: function (data, status) {
                        layer.msg('请求失败', {icon: 2});
                    }
                });
            }
            return false;  //  这句是必须的 否则会提交两次
        });

        $("#closeTab").click(function () {
            parent.$('#closeEdit').click();
        });
    });
</script>
</body>
</html>